package com.hiwan.system.service.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.framework.cache.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hiwan.platform.utils.EncryptUtil;
import com.hiwan.system.cache.SysCacheService;
import com.hiwan.system.cache.UserCacheService;
import com.hiwan.system.common.base.GenericService;
import com.hiwan.system.dao.sequence.SeqDao;
import com.hiwan.system.dao.user.UserDao;
import com.hiwan.system.pojo.MenuInfo;
import com.hiwan.system.pojo.OrgInfo;
import com.hiwan.system.pojo.RoleInfo;
import com.hiwan.system.pojo.UserCacheInfo;
import com.hiwan.system.pojo.UserInfo;
import com.hiwan.system.pojo.UserMenu;
import com.hiwan.system.pojo.UserOrgDeptRole;
import com.hiwan.system.pojo.UserRelation;
import com.hiwan.system.service.org.OrgService;
import com.hiwan.system.service.role.RoleService;
import com.hiwan.system.util.ConfigUtil;
import com.hiwan.system.util.PageUtil;
import com.hiwan.system.util.UserCacheUtils;

@Service
@Transactional
public class UserService extends GenericService<UserDao,UserInfo,String> implements UserCacheService {
	
	@Autowired
	private CacheService cacheService;
	@Autowired
	private RoleService roleService;
	@Autowired
	@Lazy
	private OrgService orgService;
	@Autowired
	private UserDao userDao;
	@Autowired
	private SeqDao seqDao;
	@Autowired
	private SysCacheService sysCacheService;

	/**
	 * 获得该用户的所有菜单
	 * 
	 * @param relationId
	 * @return
	 */
	public String getPrivateAllMenu(String relationId) {
		String privateAllMenu = "";
		List<MenuInfo> menuInfoList = userDao.selectAllMenu(relationId);
		if (menuInfoList != null) {
			for (MenuInfo o : menuInfoList) {
				privateAllMenu += o.getMenuId() + "|" + o.getParentMenu() + "|"
						+ o.getMenuName() + ";";
			}
		}
		return privateAllMenu;
	}
	
	public List<MenuInfo> selectUserPrivateMenu(String relationId) {
		List<MenuInfo> menuInfoList = userDao.selectAllMenu(relationId);
		if (menuInfoList != null) {
			return menuInfoList;
		}
		return null;
	}

	/**
	 * 获得维护后的菜单
	 * 
	 * @param relationId
	 * @return
	 */
	public String getPrivateMenuList(String relationId) {
		String privateMenuList = "";
		List<UserMenu> list = userDao.selectUserMenu(relationId);
		if (list != null) {
			for (UserMenu o : list) {
				if ("1".equals(o.getStatus())) {
					privateMenuList += o.getMenuId() + "|";
				}
			}
		}
		return privateMenuList;
	}

	/**
	 * 根据所属机构、部门查询用户
	 * 
	 * @param orgId
	 * @param deptId
	 * @return
	 */
	public List<UserRelation> selectUserListByDept(String orgId, String deptId) {
		if ("*".equals(orgId)) {
			orgId = null;
		}
		if ("*".equals(deptId)) {
			deptId = null;
		}
		return userDao.selectUserListByDept(orgId, deptId);
	}

	/**
	 * 新增用户
	 * 
	 * @param userInfo
	 * @return
	 */
	public String insertOrUpdateUserInfo(UserInfo userInfo) {
		// 根据userId判断该用户是否已经存在，如果存在，新增信息变成修改信息
		String result="";
		UserInfo ui =null;
		if(StringUtils.isNotBlank(userInfo.getUserId())){
			//ui=userDao.selectOne(userInfo);
			ui= new UserInfo();
			ui.setUserId(userInfo.getUserId());
		}
		
		String loginUserId = UserCacheUtils.getId();
		List<String> roleIds=getRoleIds(userInfo.getRoles());
		
		if (ui == null) {//新增
			userInfo.setCreateUser(loginUserId);
			//String newUserId = userInfo.getOrgId()+ StringUtils.leftPad(selectNextUserId(), 3, "0");// 新建用户需要的UserId，但用户Id为自动生成时？
			String newUserId = userInfo.getUserCode();
			userInfo.setUserId(newUserId);
			String initialPwd = sysCacheService.getSysParameter("init_pwd","*", "*").getParam1();
			userInfo.setUserPwd(EncryptUtil.entryptPassword(initialPwd));
			
			int hw_user_info_flag=userDao.insert(userInfo);
			int hw_user_role_flag=0;
			//前台获取角色列表 ，插入hw_user_role表中
			for(String roleId:roleIds){
				 Map<String,String> map=new HashMap<String,String>();
				 map.put("userId",newUserId);
			 	 map.put("roleId",roleId);
			 	 hw_user_role_flag=userDao.insertUserRoleList(map);
			}
			if(hw_user_info_flag>0&&hw_user_role_flag>0){
				result="success";
			}else{
				result="fail";
			}
		} else {
			String userId=userInfo.getUserId();
			userInfo.setUpdateUser(loginUserId);
			int hw_user_info_update_flag =userDao.update(userInfo);
			int hw_user_role_delete_flag=userDao.deleteUserRoles(userId);
			for(String roleId:roleIds){
				 Map<String,String> map=new HashMap<String,String>();
				 map.put("userId",userId);
			 	 map.put("roleId",roleId);
				 userDao.insertUserRoleList(map);
			}
			//UserCacheUtils.clearCache(userInfo);
			
			if(hw_user_info_update_flag>0&&hw_user_role_delete_flag>0){
				result="success";
			}else{
				result="fail";
			}
			
		}
		UserCacheUtils.clearCache(userInfo);
		return result;
	}
	/**
	 * 获得下一个可用的userid
	 * 
	 * @param userStr
	 * @return
	 */
	public String selectNextUserId() {

		return String.valueOf(seqDao.getAndIncreaseSeq("user_id","orgId"));
	}
	/**
	 * 得到所有角色的ID
	 * @date 2016年1月15日 上午11:15:07
	 * @author 李仕波
	 */
	public List<String>  getRoleIds(List<RoleInfo> list) {
		List<String> results=new ArrayList<String>();
		if (list != null) {
			for (RoleInfo ri : list) {
				String roleId = ri.getRoleId();
				if (StringUtils.isNotBlank(roleId)) {
					results.add(roleId);
				}
			}
			if(results==null||results.isEmpty()){
				String defaultRoleId = ConfigUtil.getConfig("defaultRoleId");
				results.add(defaultRoleId);
			}
		}else{
			String defaultRoleId = ConfigUtil.getConfig("defaultRoleId");
			results.add(defaultRoleId);
		}
		return results;
	}
	/**
	 * 删除用户
	 * @date 2016年1月15日 上午11:34:18
	 * @author 李仕波
	 */
	public String deleteUserInfo(UserInfo info) {
		String userId=info.getUserId();
		int userDeleteFlag=userDao.delete(userId);
		int userRoleDeleteFlag=userDao.deleteUserRoles(userId);
		if(userDeleteFlag>0&&userRoleDeleteFlag>0){
			return "success";
		}else{
			return "fail";
		}
	}

	

	/**
	 * 按用户编号，机构号，部门号查找关系表
	 * 
	 * @param ur
	 * @return
	 */
	public String selectRelationBy3Id(UserRelation ur) {
		UserRelation ur1 = userDao.selectRelationBy3Id(ur);
		if (ur1 != null) {
			return "1";
		} else {
			return "0";
		}
	}

	/**
	 * 个人菜单维护
	 * 
	 * @param userRoles
	 * @param relationId
	 * @return
	 */
	public String userMenuApply(String[] userRoles, String relationId) {
		String menuStr = "";
		for (int i = 0; i < userRoles.length; i++) {
			RoleInfo roleInfo = new RoleInfo();
			roleInfo.setRoleId(userRoles[i]);
			// 根据角色获得的菜单
			menuStr += roleService.getRoleMenuList(roleInfo.getRoleId());
		}
		String[] menus = menuStr.split("\\|");
		List<UserMenu> list = new ArrayList<UserMenu>();
		if (!"".equals(menuStr)) {
			for (int i = 0; i < menus.length; i++) {
				UserMenu userMenu = new UserMenu();
				userMenu.setRelationId(relationId);
				userMenu.setMenuId(menus[i]);
				userMenu.setStatus("1");
				list.add(userMenu);
			}
		}
		// 插入之前，先删除原有的
		userDao.deleteUserMenu(relationId);
		for (int i = 0; i < list.size(); i++) {
			userDao.deleteUserMenuBy2Id(list.get(i));
			userDao.insertUserMenu(list.get(i));
		}
		return "success";
	}

	/**
	 * 修改个人菜单
	 * 
	 * @param relationId
	 * @param privateMenuList
	 * @return
	 */
	public String updateUserMenu(String relationId, String privateMenuList) {
		// 根据renlationId查找自定义菜单
		List<UserMenu> list = userDao.selectUserMenu(relationId);
		if (list != null&&!list.isEmpty()) {
			for (int i = 0; i < list.size(); i++) {
				if (privateMenuList.contains(list.get(i).getMenuId() + ",")) {
					list.get(i).setStatus("1");
				} else {
					list.get(i).setStatus("0");
				}
			}
		}
		// 插入之前，先删除原有的
		userDao.deleteUserMenu(relationId);
		for (int i = 0; i < list.size(); i++) {
			userDao.deleteUserMenuBy2Id(list.get(i));
			userDao.insertUserMenu(list.get(i));
		}
		return "success";
	}

	/**
	 * 修改角色对应菜单，批量修改用户对应菜单
	 * 
	 * @param roleInfo
	 * @return (0--成功，1--失败)
	 */
	public int updateUserMenuByRoleInfo(RoleInfo roleInfo) {
		// 原来已经存在的菜单
		int j = 0;
		int k = 0;
		List<UserMenu> oriMenu = new ArrayList<UserMenu>();
		// 修改后的菜单
		List<UserMenu> laterMenu = new ArrayList<UserMenu>();
		// 求差集
		List<UserMenu> addMenu = new ArrayList<UserMenu>();
		List<UserMenu> deleteMenu = new ArrayList<UserMenu>();
		/*
		 * // 根据角色获得该角色修改后的菜单 String menuStr =
		 * roleService.getRoleMenuList(roleInfo); String[] menus =
		 * menuStr.split("\\|"); // 将数组转换为List for (int i = 0; i < menus.length;
		 * i++) { UserMenu um = new UserMenu(); um.setMenuId(menus[i]);
		 * um.setRelationId(""); um.setStatus(""); laterMenu.add(um); }
		 */
		// 根据角色获得拥有该角色的用户（UserId）
		
		UserOrgDeptRole uodr = new UserOrgDeptRole();
		//uodr.setRoleIdstr(roleInfo.getRoleId() + ",");@version 2.0
		uodr.setRoleIdstr(roleInfo.getRoleId());
		List<UserOrgDeptRole> uodrList = userDao.selectUserListNoPage(uodr);
		// 根据UserId循环修改用户菜单关系表（原来存在的不做修改，没有的新增，多余的删除）
		for (UserOrgDeptRole a : uodrList) {
			// 根据renlationId查找自定义菜单
			laterMenu = getUnionMenu(a);
			addMenu.clear();
			deleteMenu.clear();
			oriMenu = userDao.selectUserMenu(a.getRelationId());
			for (int i = 0; i < laterMenu.size(); i++) {
				for (j = 0; j < oriMenu.size(); j++) {
					if (laterMenu.get(i).getMenuId()
							.equals(oriMenu.get(j).getMenuId())) {
						break;
					}
				}
				if (j == oriMenu.size()) {
					UserMenu uu = new UserMenu();
					uu.setMenuId(laterMenu.get(i).getMenuId());
					addMenu.add(uu);
				}
			}
			for (int i = 0; i < oriMenu.size(); i++) {
				for (k = 0; k < laterMenu.size(); k++) {
					if (oriMenu.get(i).getMenuId()
							.equals(laterMenu.get(k).getMenuId())) {
						break;
					}
				}
				if (k == laterMenu.size()) {
					UserMenu ui = new UserMenu();
					ui.setMenuId(oriMenu.get(i).getMenuId());
					deleteMenu.add(ui);
				}
			}
			// 新增
			for (int i = 0; i < addMenu.size(); i++) {
				addMenu.get(i).setRelationId(a.getRelationId());
				addMenu.get(i).setStatus("1");
				try {
					if (userDao.insertUserMenu(addMenu.get(i)) < 1) {
						return 1;
					}
					;
				} catch (Exception e) {
					return 1;
				}

			}
			// 删除
			for (int i = 0; i < deleteMenu.size(); i++) {
				deleteMenu.get(i).setRelationId(a.getRelationId());
				try {
					if (userDao.deleteUserMenuBy2Id(deleteMenu.get(i)) < 1) {
						return 1;
					}
					;
				} catch (Exception e) {
					return 1;
				}
			}

		}
		return 0;
	}

	/**
	 * 获得该用户下所有角色所拥有角色的并集
	 * 
	 * @return
	 */
	public List<UserMenu> getUnionMenu(UserOrgDeptRole uodr) {

		List<UserMenu> laterMenu = new ArrayList<UserMenu>();
		Set<String> set = new HashSet<String>();

		String roleIdstr = uodr.getRoleIdstr();
		String[] roles = roleIdstr.split(",");
		for (int i = 0; i < roles.length; i++) {
			RoleInfo roleInfo = new RoleInfo();
			roleInfo.setRoleId(roles[i]);
			List<String>  menuStr = roleService.getRoleMenuList(roleInfo.getRoleId());
			/*if (!"".equals(menuStr)) {
				for (int j = 0; j < menus.length; j++) {
					String str = new String(menus[j]);
					set.add(str);
				}
			}*/
			for(String str:menuStr){
				set.add(str);
			}
		}
		Iterator<String> ss = set.iterator();// 先迭代出来
		while (ss.hasNext()) {// 遍历
			UserMenu um = new UserMenu();
			um.setMenuId((String) ss.next());
			um.setRelationId("");
			um.setStatus("");
			laterMenu.add(um);
		}
		return laterMenu;
	}

	/**
	 * 修改用户
	 * 
	 * @param userInfo
	 * @return
	 */
	public String updateUserInfo(UserInfo userInfo,
			UserRelation userRelation, String userId, String[] userRoles,
			String defaultRole) {
		String result = "";
		if(userInfo.getUserId()==null){
			String userIdd=selectByUserRelation(userRelation.getRelationId()).getUserId();
			userInfo.setUserId(userIdd);
		}
		userInfo.setUpdateUser(userId);
		result = updateUserInfo(userInfo);
		if ("success".equals(result)) {
			userRelation.setUpdateUser(userId);
			result = updateUserRelation(userRelation, userRoles, defaultRole);
			userMenuApply(userRoles, userRelation.getRelationId());
		}
		return result;
	}

	public String updateUserInfo(UserInfo userInfo) {
		int flag = userDao.update(userInfo);
		// 更新缓存
		cacheService.remove("userCache", userInfo.getUserId());
		if (flag > 0) {
			return "success";
		} else {
			return "fail";
		}
	}

	/**
	 * 删除用户
	 * 
	 * @param userId
	 * @return
	 */
	public String deleteUserInfo(String userId, String relationId, String status) {
		UserInfo user=new UserInfo();
		user.setUserId(userId);
		String orgId = userDao.selectOne(user).getOrgId();
		int flag1 = 0;
		int flag2 = 0;
		// 删除用户关系表
		flag1 = userDao.deleteUserRelation(relationId);
		if (userDao.selectUserRelations(userId).size() == 0&& "0".equals(status)) {
			flag2 = userDao.delete(userId);
			// 删除缓存
			cacheService.remove("userCache", userId);
		}
		// 删除相对应用户菜单表
		userDao.deleteUserMenu(relationId);
		if (flag1 > 0 && flag2 > 0) {
			orgService.removeOrgInfo(orgId);
			return "success";
		} else {
			return "fail";
		}
	}

	/**
	 * 删除用户关系
	 * 
	 * @param relationId
	 * @return
	 */
	public String deleteUserRelation(String relationId) {
		int flag = userDao.deleteUserRelation(relationId);
		if (flag > 0) {
			return "success";
		} else {
			return "fail";
		}
	}

	/**
	 * 按身份证等用户自身条件查找用户列表
	 * 
	 * @param userInfo
	 * @return
	 */
	public UserInfo selectUserInfoByCard(UserInfo userInfo) {
		return userDao.selectUserInfoByCard(userInfo);
	}

	/**
	 * 插入用户关系表
	 * 
	 * @param userRelation
	 * @param userRoles
	 * @return
	 */
	public String insertUserRelation(UserRelation userRelation,
			String[] userRoles, String defaultRole, String seq) {
		userRelation.setRelationId(seq);
		String roleIdstr = "";
		for (int i = 0; i < userRoles.length; i++) {
			roleIdstr += userRoles[i] + ",";
		}
		roleIdstr = roleIdstr.replaceAll(defaultRole + ",", "");
		roleIdstr = defaultRole + "," + roleIdstr;
		userRelation.setRoleIdstr(roleIdstr);
		int t = userDao.insertUserRelation(userRelation);
		if (t > 0) {
			return "success";
		} else {
			return "fail";
		}
	}

	/**
	 * 修改用户关系表
	 * 
	 * @param userRelation
	 * @param userRoles
	 * @param defaultRole
	 * @return
	 */
	public String updateUserRelation(UserRelation userRelation,
			String[] userRoles, String defaultRole) {

		String roleIdstr = "";
		for (int i = 0; i < userRoles.length; i++) {
			roleIdstr += userRoles[i] + ",";
		}
		roleIdstr = roleIdstr.replaceAll(defaultRole + ",", "");
		roleIdstr = defaultRole + "," + roleIdstr;
		userRelation.setRoleIdstr(roleIdstr);
		int t = userDao.updateUserRelation(userRelation);
		if (t > 0) {
			return "success";
		} else {
			return "fail";
		}
	}
	/**
	 * 改变角色
	 * @param userInfo
	 * @return
	 */
	public int updateUserDefaultRole(UserInfo userInfo){
		return userDao.updateUserDefaultRole(userInfo);
	}
	/**
	 * 
	 * @author lsb
	 * @since  2015-11-17 下午7:47:48
	 * @version hiwan 3.0	
	 * @param userRelation
	 * @return
	 */
	public int updateUserRelation(UserRelation userRelation) {
		int t = userDao.updateUserRelation(userRelation);
		return t;
	}

	/**
	 * 首选修改
	 * 
	 * @param userRelation
	 * @return
	 */
	public String updateReOrder(UserRelation userRelation) {
		int flag = userDao.updateOneReOrder(userRelation);
		userDao.updateReOrderOthers(userRelation);
		if (flag > 0) {
			return "success";
		} else {
			return "fail";
		}
	}

	/**
	 * 修改密码
	 * 
	 * @param userInfo
	 * @return
	 */
	public String updatePwd(UserInfo userInfo, String oldPwd) {
		UserInfo ui = userDao.selectOne(userInfo);
		if (!EncryptUtil.validatePassword(oldPwd, ui.getUserPwd())) {
			return "fail1";
		} else {
			userInfo.setUserPwd(EncryptUtil.entryptPassword(userInfo.getUserPwd()));
			int t = userDao.updatePwd(userInfo);
			if (t > 0) {
				UserCacheUtils.clearCache(ui);
				return "success";
			} else {
				return "fail2";
			}
		}

	}

	/**
	 * 重置密码
	 * 
	 * @param userInfo
	 * @return
	 */
	public String resetPwd(UserInfo userInfo) {
		String initialPwd = sysCacheService.getSysParameter("init_pwd", "*","*").getParam1();
		//Md5PasswordEncoder md5 = new Md5PasswordEncoder();
		userInfo.setUserPwd(EncryptUtil.entryptPassword(initialPwd));
		int t = userDao.updatePwd(userInfo);
		if (t > 0) {
			return "success";
		} else {
			return "fail";
		}
	}
	

	/**
	 * 根据RelationId查询userRelation信息
	 * 
	 * @param relationId
	 * @return
	 */
	public UserRelation selectByUserRelation(String relationId) {
		return userDao.selectByRelationId(relationId);
	}

	/**
	 * 根据用户ID查询用户信息
	 * 
	 * @param userId
	 * @return
	 */
	public UserInfo getUserInfo(String userId) {
		if(userId==null){
			return null;
		}
		UserInfo u=new UserInfo();
		u.setUserId(userId);
		return userDao.selectOne(u);
	}

	/**
	 * 根据用户ID查询用户信息
	 * 
	 * @param userId
	 * @return
	 */
	public boolean isAliasUsed(String aliasName, String userId) {
		UserInfo user = new UserInfo();
		user.setUserName(aliasName);
		UserInfo userInfo = userDao.selectOne(user);
		if (userInfo != null ) {
			return true;
		}
		return false;
	}

	/**
	 * 根据userId查找用户关系表，可用来判断是否已经有该用户
	 * 
	 * @param userId
	 * @return
	 */
	public List<UserRelation> selectUserRelations(String userId) {
		if(userId==null||userId.equals("")){
			return userDao.selectUserRelations(userId);
		}
		return userDao.selectUserRelations(userId);
	}

	

	/**
	 * 用户信息加入缓存
	 * 
	 * @param userId
	 * @return
	 */
	@Override
	public UserCacheInfo getUserCacheInfo(String userId) {
		UserCacheInfo userCacheInfo = null;
		UserInfo u=new UserInfo();
		u.setUserId(userId);
		try {
			userCacheInfo = (UserCacheInfo) cacheService.get("userCache",
					userId);
		} catch (NoSuchFieldException e) {
			UserInfo userInfo = userDao.selectOne(u);
			List<UserRelation> userRelations = userDao.selectUserRelations(
					userId, "1");
			userCacheInfo = UserCacheInfo.cast2UserCache(userInfo,
					userRelations);
			// 以id和别名为key存储两份缓存副本
			if (userInfo != null) {
				cacheService.put("userCache", userInfo.getAliasName(),
						userCacheInfo);
			}
			cacheService.put("userCache", userId, userCacheInfo);
		}
		return userCacheInfo;
	}

	/**
	 * 根据用户id从缓存中取用户姓名
	 * 
	 * @param userId
	 * @return
	 */
	@Override
	public String getUserName(String userId) {
		UserCacheInfo userInfo = getUserCacheInfo(userId);
		if (userInfo != null) {
			return userInfo.getUserName();
		} else {
			return "";
		}
	}

	@Override
	public List<UserRelation> getUserRelations(String userId) {
		UserCacheInfo userInfo = getUserCacheInfo(userId);
		if (userInfo != null) {
			return userInfo.getUserRelations();
		} else {
			return new ArrayList<UserRelation>();
		}
	}

	/**
	 * 根据角色机构查询该部门下所有用户列表
	 * 
	 * @param orgId
	 * @param roleIdstr
	 * @return
	 */
	public List<UserRelation> selectUserListByRole(String orgId,
			String roleIdstr) {
		return userDao.selectUserListByRole(orgId, roleIdstr);
	}

	@Override
	public void sysInit() {
		/*
		 * String userId = ""; // 所有userId 和 userName List<UserInfo> userList =
		 * userDao.selectAllUsers(); logger.info("---------正在初始化用户---------");
		 * for (UserInfo userInfo : userList) { userId = userInfo.getUserId();
		 * List<UserRelation> userRelations = new ArrayList<UserRelation>();
		 * userRelations = userDao.selectUserRelations(userId); UserCacheInfo
		 * userCacheInfo = new UserCacheInfo(); userCacheInfo.setUserId(userId);
		 * userCacheInfo.setUserName(userInfo.getUserName());
		 * userCacheInfo.setUserRelations(userRelations);
		 * cacheService.put("userCache", userId, userCacheInfo); }
		 * logger.info("---------用户初始化成功！---------"); //
		 * HiwanFrameworkBaseUtil.getData().put("sysDictData", dictData);
		 */
		}
	
	/**
	 * ~~~~~~~~~~~~~~~~~  新方法   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	 */
	
	/**
	 * 按查询条件查询用户列表
	 * @param info
	 * @return
	 */
	public List<UserInfo> selectUserListPage(UserInfo info) {
		 List<UserInfo> list = (List<UserInfo>) dao.selectListPage(info,PageUtil.getRowBounds());
		 for(UserInfo in:list){
			 List<RoleInfo> roles = dao.selectUserRoles(in);
			 in.setRoles(roles);
		 }
		return list;
	}
	public List<UserInfo> selectUserList(UserInfo info) {
		List<UserInfo> list = (List<UserInfo>)dao.selectList(info);
		for(UserInfo in:list){
			 List<RoleInfo> roles = dao.selectUserRoles(in);
			 in.setRoles(roles);
		 }
		return list;
	}
	
	/**
	 * 
	 */
	public List<UserInfo> selectUserByOrgIdList(UserInfo info){
		return (List<UserInfo>)dao.selectUserByOrgIdList(info);
	}
	
	public List<UserInfo> selectUserListByUser(UserInfo info) {
		List<UserInfo> list = userDao.selectByUserId(info);
		for(UserInfo in:list){
			 List<RoleInfo> roles = dao.selectUserRoles(in);
			 in.setRoles(roles);
		 }
		return list;
	}
	public UserInfo selectUserInfo(UserInfo info) {
		return  dao.selectOne(info);
	}
	public List<UserInfo> selectUserMany(UserInfo info) {
		return  dao.selectUserMany(info);
	}
	public List<UserInfo> getUserInfoList() {
		return  dao.getUserInfoList();
	}
	public int insertUserFromXXY(UserInfo info){
		return dao.insertUserFromXXY(info);
	}
	public int updateUserFromXXY(UserInfo info){
		return dao.updateUserFromXXY(info);
	}
	public int updateUserRoleFromXXY(UserInfo info){
		return dao.updateUserRoleFromXXY(info);
	}
	public int deleteUserOneRoleFromXXY(UserInfo info){
		return dao.deleteUserOneRoleFromXXY(info);
	}
	
	public int insertUserRoleFromXXY(UserInfo info){
		return dao.insertUserRoleFromXXY(info);
	}
	public UserInfo getUserRole(UserInfo info){
		return dao.getUserRole(info);
	}
	public OrgInfo getUserOrgInfo(UserInfo user){
		return dao.getUserOrgInfo(user);
	}
	public List<UserInfo> getUserInfo(UserInfo user){
		List<UserInfo> userInfos=dao.getUserInfos(user);
		return userInfos;
	}
}
